<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>WebSocket 测试页面</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.7.2/css/all.min.css" rel="stylesheet">
</head>
<body class="bg-gray-100 font-sans">
<div class="container mx-auto p-4">
    <h1 class="text-2xl font-bold mb-4">WebSocket 测试</h1>
    <div class="mb-4">
        <label for="userId" class="block text-sm font-medium text-gray-700">用户 ID:</label>
        <input type="text" id="userId" class="mt-1 block w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-blue-500 focus:border-blue-500 sm:text-sm">
    </div>
    <button id="connectButton" class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded mb-4" onclick="connectWebSocket()">
        连接 WebSocket
    </button>
    <div class="mb-4">
        <label for="message" class="block text-sm font-medium text-gray-700">消息内容:</label>
        <input type="text" id="message" class="mt-1 block w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-blue-500 focus:border-blue-500 sm:text-sm">
    </div>
    <button id="sendButton" class="bg-green-500 hover:bg-green-700 text-white font-bold py-2 px-4 rounded mb-4" onclick="sendMessage()" disabled>
        发送消息
    </button>
    <div class="bg-white p-4 rounded-md shadow-md">
        <h2 class="text-lg font-bold mb-2">消息记录</h2>
        <div id="messageLog" class="max-h-64 overflow-y-auto"></div>
    </div>
</div>
<script>
    let socket;

    function connectWebSocket() {
        const userId = document.getElementById('userId').value;
        if (!userId) {
            alert('请输入用户 ID');
            return;
        }
        socket = new WebSocket(`ws://localhost:8080/websocket/${userId}`);

        socket.onopen = function() {
            document.getElementById('connectButton').disabled = true;
            document.getElementById('sendButton').disabled = false;
            addMessageToLog('已连接到 WebSocket 服务器');
        };

        socket.onmessage = function(event) {
            addMessageToLog(`收到消息: ${event.data}`);
        };

        socket.onclose = function() {
            document.getElementById('connectButton').disabled = false;
            document.getElementById('sendButton').disabled = true;
            addMessageToLog('与 WebSocket 服务器的连接已关闭');
        };

        socket.onerror = function(error) {
            addMessageToLog(`WebSocket 发生错误: ${error.message}`);
        };
    }

    function sendMessage() {
        const message = document.getElementById('message').value;
        if (!message) {
            alert('请输入消息内容');
            return;
        }
        if (socket && socket.readyState === WebSocket.OPEN) {
            socket.send(message);
            addMessageToLog(`已发送消息: ${message}`);
            document.getElementById('message').value = '';
        } else {
            alert('未连接到 WebSocket 服务器，请先连接');
        }
    }

    function addMessageToLog(message) {
        const logDiv = document.getElementById('messageLog');
        const newMessage = document.createElement('p');
        newMessage.textContent = message;
        logDiv.appendChild(newMessage);
    }
</script>
</body>
</html>
